package com.tiger.web.controller.bill;

import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.framework.web.service.TokenService;
import com.tiger.project.domain.AssetReceipt;
import com.tiger.project.domain.AssetReceiptHistory;
import com.tiger.project.domain.ContractBill;
import com.tiger.project.service.IAssetReceiptService;
import com.tiger.common.annotation.Log;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.page.TableDataInfo;
import com.tiger.common.enums.BusinessType;
import com.tiger.project.service.IContractBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import io.swagger.annotations.ApiParam;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 【租金收款/租金进账】Controller
 *
 * @author standard
 * @date 2022-09-27
 */
@Api(tags = "开票申请/租金进账")
@RestController
@RequestMapping("/system/receipt")
public class AssetReceiptController extends BaseController {
    @Autowired
    private IAssetReceiptService assetReceiptService;
    @Autowired
    private IContractBillService contractBillService;


    @ApiOperation(value = "查询租金进账/租金到账 列表")
    //@PreAuthorize("@ss.hasPermi('system:receipt:list')")
    @GetMapping("/listRecord")
    public TableDataInfo listRecord(ContractBill contractBill) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        contractBill.setDeptId(loginUser.getUser().getDeptId());
        startPage();
        // 开票申请，租金进账，租金到账为同一个列表
        contractBill.setStatus(3L);
        List<ContractBill> list = contractBillService.payeeEarlyList(contractBill);
        return getDataTable(list);
    }

    /**
     * 租金进账/到账 添加
     * @param contractBill
     * @return
     */
    @ApiOperation(value = "租金进账/到账 缴费")
    @PutMapping("/editRecord")
    public AjaxResult editRecord(@RequestBody ContractBill contractBill){
        return toAjax(contractBillService.editRecord(contractBill));
    }


    /**
     * 收款预警列表/开票申请
     */
    @ApiOperation(value = "收款预警列表/开票申请")
    @GetMapping("/payeeEarlyList")
    public TableDataInfo payeeEarlyList(ContractBill contractBill){
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        contractBill.setDeptId(loginUser.getUser().getDeptId());
        startPage();
        List<ContractBill> list = contractBillService.payeeEarlyList(contractBill);
        return getDataTable(list);
    }


    /**
     * 开票信息详情，进账，到账
     */
    @ApiOperation(value = "开票信息申请详情-进账-到账")
    @GetMapping("/payeeEarlyInfo")
    public AjaxResult payeeEarlyInfo(@ApiParam(value = "账期id") Long id){
        return AjaxResult.success(contractBillService.selectContractBillById(id));
    }

    /**
     * 删除开票申请，把该条开票信息清空
     */
    @ApiOperation(value = "删除开票信息")
    @DeleteMapping("/deletePayee")
    public AjaxResult deletePayee(Long id){
        return toAjax(contractBillService.deletePayee(id));
    }

    /**
     * 操作 开票申请
     */
    @ApiOperation(value = "开票申请")
    @PutMapping("/updatePayeeEarly")
    public AjaxResult updatePayeeEarly(@RequestBody ContractBill contractBill){
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        contractBill.setApplyId(loginUser.getUserId());
        contractBill.setAssetName(loginUser.getUser().getNickName());
        return toAjax(contractBillService.updateContractBill(contractBill));
    }


    @ApiOperation(value = "OA开票申请审核回调")
    @GetMapping("/callBack")
    public AjaxResult callBack(@ApiParam(value = "租金收款id") Long id,
                               @ApiParam(value = "审核状态 3-审批通过，4-已驳回") Long status,@ApiParam(value = "OA审批文件链接") String oaFile,
                               @ApiParam(value = "财务部-负责人是否开票") String isMakeInvoice,@ApiParam(value = "发起人是否拿票") String isGetInvoice){
        return toAjax(contractBillService.callBack(id, status, oaFile, isMakeInvoice, isGetInvoice));
    }


    @ApiOperation(value = "租金进账/到账详情")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(@ApiParam(value = "账期id")Long id,
                              @ApiParam(value = "类型 1-缴款(收款)  2-到账") Long payType){
        return AjaxResult.success(contractBillService.getInfo(id, payType));
    }


    /**
     * 删除租金到账
     * @param id
     * @return
     */
    @ApiOperation(value = "删除租金到账")
    @DeleteMapping(value = "/deleteRecord")
    public AjaxResult deleteRecord(Long id){
        return toAjax(contractBillService.deleteRecord(id));
    }

    /**
     * 删除租金进账
     */
    @ApiOperation(value = "删除租金进账")
    @DeleteMapping(value = "/deleteArrival")
    public AjaxResult deleteArrival(Long id){
        return toAjax(contractBillService.deleteArrival(id));
    }

    /**
     * 租金到账OA审核
     */
    @ApiOperation(value = "租金到账OA审核")
    @GetMapping("/callBackRecord")
    public AjaxResult callBackRecord(@ApiParam(value = "账期id") Long id,@ApiParam(value = "租金到账审核状态") Long recordStatus,
                                     @ApiParam(value = "OA审批文件链接") String oaFile){
        return toAjax(contractBillService.callBackRecord(id, recordStatus, oaFile));
    }

    /**
     * 租金进账OA审批
     */
    @ApiOperation(value = "租金进账OA审批")
    @GetMapping("/callBackArrival")
    public AjaxResult callBackArrival(@ApiParam(value = "账期id") Long id,@ApiParam(value = "租金进账审核状态") Long arrivalStatus,
                                      @ApiParam(value = "OA审批文件链接") String oaFile){
        return toAjax(contractBillService.callBackArrival(id, arrivalStatus, oaFile));
    }





}
